﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;
using TarreOA.TarreOADAL;
using System.Data.SqlClient;

public partial class User_UserEmail_EmailInfoShow : System.Web.UI.Page
{
    private EmpEmailMappingtbService empEmailMappingtbService = new EmpEmailMappingtbService();
    private string type;
    private PagedDataSource pds;
    private int days = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["type"] != null)
        {
            this.type = Request["type"];
        }
        else
        {
            type = "incept";
        }
        if (Request.Params.Get("days") != null)
        {
            days = int.Parse(Request.Params.Get("days"));
        }
        if (!Page.IsPostBack)
        {
            this.NewPagedDataSource();
            dataListDataBind();
            ViewState["pg_in"] = pds.CurrentPageIndex;
        }
    }

    private void NewPagedDataSource()
    {
        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = 8;
        dataListTable();
    }

    protected void dataListDataBind()
    {
        this.DataList1.DataSource = pds;
        DataList1.DataBind();
    }

    protected void dataListTable()
    {
        //DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("EmailId", typeof(string));
        dt.Columns.Add("ReadState", typeof(string));
        dt.Columns.Add("SendUserLoginName", typeof(string));
        dt.Columns.Add("InceptUserLoginName", typeof(string));
        dt.Columns.Add("EmailContent", typeof(string));
        dt.Columns.Add("SendDateTime", typeof(string));
        User curLoginUser = (User)Session["user"];
        List<EmpEmailMappingtb> list = null;
        if (curLoginUser == null)
        {
            Response.Redirect("../../Login.aspx");
        }
        else
        {
            if ("incept".Equals(this.type.Trim()))
            {
                list = empEmailMappingtbService.findByInceptUserLoginName(curLoginUser.LoginName);
            }
            else
            {
                list = empEmailMappingtbService.findBySendUserLoginName(curLoginUser.LoginName);
            }
            if (list != null && list.Count > 0)
            {
                foreach (EmpEmailMappingtb empEmailMapping in list)
                {
                    DataRow dr = dt.NewRow();
                    if (days == 0)
                    {
                        dr["EmailId"] = empEmailMapping.EmpEmailId.ToString();
                        string readState = "New";
                        if (empEmailMapping.ReadState == 1)
                        {
                            readState = "";
                        }
                        dr["ReadState"] = readState;
                        dr["SendUserLoginName"] = empEmailMapping.SendUserName;
                        dr["InceptUserLoginName"] = empEmailMapping.InceptUserName;
                        string emailContent = empEmailMapping.EmialTextContent;
                        if (emailContent.Length > 30)
                        {
                            emailContent = emailContent.Substring(0, 30) + "...";
                        }
                        dr["EmailContent"] = emailContent;
                        dr["SendDateTime"] = empEmailMapping.SendDateTime.ToString();
                    }
                    else
                    {
                        if (empEmailMapping.SendDateTime >= DateTime.Now.AddDays(-days))
                        {
                            dr["EmailId"] = empEmailMapping.EmpEmailId.ToString();
                            string readState = "New";
                            if (empEmailMapping.ReadState == 1)
                            {
                                readState = "";
                            }
                            dr["ReadState"] = readState;
                            dr["SendUserLoginName"] = empEmailMapping.SendUserName;
                            dr["InceptUserLoginName"] = empEmailMapping.InceptUserName;
                            string emailContent = empEmailMapping.EmialTextContent;
                            if (emailContent.Length > 30)
                            {
                                emailContent = emailContent.Substring(0, 30) + "...";
                            }
                            dr["EmailContent"] = emailContent;
                            dr["SendDateTime"] = empEmailMapping.SendDateTime.ToString();
                        }
                        else
                        {
                            dr = null;
                        }
                    }
                    if (dr != null)
                    {
                        dt.Rows.Add(dr);
                    }
                }
            }
            //ds.Tables.Add(dt);
            //pds.DataSource = ds.Tables[0].DefaultView;
            pds.DataSource = dt.DefaultView;
        }
    }
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            if ("incept".Equals(this.type))
            {
                //((Label)e.Item.FindControl("lblSendOrInceptUser")).Text = "发送人:";
                ((Label)e.Item.FindControl("lblInceptUserName")).Visible = false;
            }
            else
            {
               // ((Label)e.Item.FindControl("lblSendOrInceptUser")).Text = "接收人:";
               ((Label)e.Item.FindControl("lblSendUserName")).Visible = false;
            }
            ((HyperLink)e.Item.FindControl("hplInform")).NavigateUrl = "EmailContent.aspx?eid=" + ((HtmlInputHidden)e.Item.FindControl("hiddEid")).Value + "&type=" + this.type;
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {
            if (pds.IsFirstPage)
            {
                ((Button)e.Item.FindControl("btnRetu")).Enabled = false;
                ((Button)e.Item.FindControl("btnGoTop")).Enabled = false;
            }
            if (pds.IsLastPage)
            {
                ((Button)e.Item.FindControl("btnNext")).Enabled = false;
                ((Button)e.Item.FindControl("btnGoLast")).Enabled = false;
            }
        }
        if (e.Item.ItemType == ListItemType.Footer)
        {
            ((Label)e.Item.FindControl("lblPageState")).Text = "当前第<font color='red'><b>" + (pds.CurrentPageIndex + 1) + "</b></font>页/共<font color='red'><b>" + pds.PageCount + "</b></font>页"; 
        }
    }
    protected void btnRetu_Click(object sender, EventArgs e)
    {
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        pds.CurrentPageIndex--;
        dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        pds.CurrentPageIndex++;
        dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
    }
    protected void btnGoTop_Click(object sender, EventArgs e)
    {
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        pds.CurrentPageIndex = 0;
        dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
    }
    protected void btnGoLast_Click(object sender, EventArgs e)
    {
        this.NewPagedDataSource();
        if (ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int)ViewState["pg_in"];
        }
        pds.CurrentPageIndex = pds.PageCount - 1;
        dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
    }
    protected void ddlFindDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = 8;
        //this.days = int.Parse(this.ddlFindDate.SelectedValue);
        this.dataListTable();
        this.dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
    }
    protected void btnDelMsg_Click(object sender, EventArgs e)
    {
        SqlConnection sqlConn = SQLManager.GetConn();
        sqlConn.Open();
        string sql = "delete from ";
        SqlCommand sqlCmd = null;
        if(Request.Params.GetValues("chkCur") != null && "incept".Equals(Request["type"]))
        {
            string[] delEids = Request.Params.GetValues("chkCur");
            sql += "EmpEmailMappingtb2 where EmpEmailId in (";
            for(int i = 0; i < delEids.Length; i++)
            {
                sql += (delEids[i] + ",");
            }
            sql += "0)";
            sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.ExecuteNonQuery();
        }
        else if(Request.Params.GetValues("chkCur") != null)
        {
            string[] delEids = Request.Params.GetValues("chkCur");
            sql += "EmpEmailMappingtb where EmpEmailId in (";
            for(int i = 0; i < delEids.Length; i++)
            {
                sql += (delEids[i] + ",");
            }
            sql += "0)";
            sqlCmd = new SqlCommand(sql, sqlConn);
            sqlCmd.ExecuteNonQuery();
        }
        this.NewPagedDataSource();
        if(ViewState["pg_in"] != null)
        {
            pds.CurrentPageIndex = (int) ViewState["pg_in"];
        }
        if(pds.CurrentPageIndex == pds.PageCount)
        {
            pds.CurrentPageIndex--;
        }
        this.dataListDataBind();
        ViewState["pg_in"] = pds.CurrentPageIndex;
        if(sqlConn != null && sqlConn.State == ConnectionState.Open)
        {
            sqlConn.Close();
            sqlConn.Dispose();
        }
    }
}
